/**
 * 
 */


import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Action.Action;
import Form.ActionForm;
import Util.InvokeReflectBean;

/**
 * @author Administrator
 *
 */
public class ActionServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7174273558902779925L;

	/**
	 * 
	 */
	public ActionServlet() {
		// TODO Auto-generated constructor stub
	}
	/*
	 * 核心Servlet，作为controller角色，接收前端传递的参数，并指派给处理类进行处理
	 */
	public void doGet(HttpServletRequest request,HttpServletResponse response)
	throws ServletException,IOException{
		//字符编码
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		//接收前端参数并赋值，同时封装为FormBean
		ActionForm actionForm = (ActionForm)InvokeReflectBean.InvokeBean(request);
		//System.out.println(actionForm);
		Action action = null;
		//获得处理类
		Map<String,String> map = ActionMapping.getMap();
		try{
			String ActionName = map.get(request.getParameter("FormBean"));
			Class className = Class.forName(ActionName);
			action = (Action)className.newInstance();
			
		}catch(Exception e){
			//记录异常信息
			//logger.error();
			
		}
		String message = action.Execute(actionForm);
		//message = new String(message.getBytes("utf-8"));
		//返回对页面信息编码，以处理中文乱码的问题
		PrintWriter pw = response.getWriter();
		pw.print("<html>");
		pw.print("<head>");
		pw.print("<head>");
		pw.print("<meta charset=\"UTF-8\">");
		pw.print("</head>");
		pw.print("<body>");
		pw.print(message);
		pw.print("</body>");
		pw.print("</html>");
		pw.flush();
		pw.close();
	}
	public void doPost(HttpServletRequest request,HttpServletResponse response)
	throws ServletException,IOException{
		
		this.doGet(request, response);
	}
	/**
	 * @param args
	 */
	/*
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}
	 */

}
